package com.tencent.qcloud.timchat.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;


import com.polites.android.GestureImageView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.Downloader;
import com.wbao.dianniu.utils.FileUtil;
import com.wbao.dianniu.utils.Notification;

import java.io.IOException;

public class ImageViewActivity extends Activity {
    GestureImageView imageView;
    private String imagePath;
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_image_view);
        RelativeLayout root = (RelativeLayout) findViewById(R.id.root);
        /*root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("img","root click");
                finish();
            }
        });*/
        imagePath = getIntent().getStringExtra("filename");
//        Log.e("img", "iamgePath:" + imagePath);
        imageView = (GestureImageView) findViewById(R.id.image);
        Bitmap bitmap = getImage(FileUtil.getCacheFilePath(imagePath));
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Log.e("img","imageView click");
                    finish();
                    overridePendingTransition(0, R.anim.zoom_exit);
                }
            });
            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
//                    Log.e("img","imageView long click");
                    showDialog(FileUtil.getCacheFilePath(imagePath));
                    return true;
                }
            });
        }
    }

    /**
     * 显示保存图片弹出框
     */
    private void showDialog(final String path) {
//        Log.e("img","path is:" + path);
        dialog = new Dialog(this, R.style.custom_Dialog);
        Window win = dialog.getWindow();
        win.getDecorView().setPadding(3, 3, 3, 3);
        win.setGravity(Gravity.BOTTOM);
        View view = LayoutInflater.from(this).inflate(R.layout.qa_collection_dialog, null);
        Button collectBtn = (Button) view.findViewById(R.id.qa_collection_button);
        Button cancelBtn = (Button) view.findViewById(R.id.qa_collection_cancel);
        collectBtn.setText(this.getResources().getString(R.string.save_picture));
        //保存图片
        collectBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                // 保存图片到
                Downloader.download(ImageViewActivity.this, path);
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.show();
    }


    private Bitmap getImage(String path) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int reqWidth, reqHeight, width = options.outWidth, height = options.outHeight;
        if (width > height) {
            reqWidth = getWindowManager().getDefaultDisplay().getWidth() / 2;
            reqHeight = (reqWidth * height) / width;
        } else {
            reqHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
            reqWidth = (width * reqHeight) / height;
        }
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        try {
            options.inSampleSize = inSampleSize;
            options.inJustDecodeBounds = false;
            Matrix mat = new Matrix();
            Bitmap bitmap = BitmapFactory.decodeFile(path, options);
            if (bitmap == null) {
                Notification.toast(this, getString(R.string.file_not_found));
                return null;
            }
            ExifInterface ei = new ExifInterface(path);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    mat.postRotate(90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    mat.postRotate(180);
                    break;
            }
            return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);
        } catch (IOException e) {
            return null;
        }
    }
}
